<template>
  <vxe-modal @close="cancel" title="选择物资 (状态为正常关联的物资)" v-model="wzDuoX" width="85%" showFooter show-zoom resize>
    <div style="height: 75vh; overflow-y: auto;">
      <!-- 顶部搜索 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="物资名称:" prop="wzName">
          <el-input size="mini"
                    v-model="queryParams.wzName"
                    placeholder="请输入物资名称"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="所属仓库:" prop="ckName">
          <el-input size="mini"
                    v-model="queryParams.ckName"
                    placeholder="请输入所属仓库"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="所属货架:" prop="hjName">
          <el-input size="mini"
                    v-model="queryParams.hjName"
                    placeholder="请输入所属货架"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="所属分层:" prop="fcName">
          <el-input size="mini"
                    v-model="queryParams.fcName"
                    placeholder="请输入所属分层"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物资分类:" prop="wzFl">
          <el-input size="mini"
                    v-model="queryParams.wzFl"
                    placeholder="请输入物资分类"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物资规格:" prop="wzGg">
          <el-input size="mini"
                    v-model="queryParams.wzGg"
                    placeholder="请输入物资规格"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物资专业:" prop="wzZy">
          <el-input size="mini"
                    v-model="queryParams.wzZy"
                    placeholder="请输入物资专业"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table height="55vh" @row-click="clickRow" ref="table" highlight-current-row
                :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                v-loading="loading" :data="wzxxList" @selection-change="handleSelectionChange">
        <!--      <el-table-column type="selection" width="55" align="center"/>-->
<!--        <el-table-column type="selection" width="55" align="center"/>-->
        <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
        <el-table-column show-overflow-tooltip label="物资名称" align="center" prop="wzName">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleButtonClick(scope.row)"
            >
              <el-link style="font-weight: 600" type="primary">{{ scope.row.wzName }}</el-link>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="物资单位" align="center" prop="wzDw"/>
        <!--      <el-table-column label="所属仓库" align="center" prop="ssCk"/>-->
        <el-table-column show-overflow-tooltip label="所属仓库" align="center" prop="ckName"/>
        <!--      <el-table-column label="所属货架" align="center" prop="ssHj"/>-->
        <el-table-column show-overflow-tooltip label="所属货架" align="center" prop="hjName"/>
        <!--      <el-table-column label="所属分层" align="center" prop="hjName"/>-->
        <el-table-column show-overflow-tooltip label="所属分层" align="center" prop="fcName"/>
        <el-table-column show-overflow-tooltip label="物资分类" align="center" prop="wzFl"/>
        <el-table-column show-overflow-tooltip label="安全库存" align="center" prop="aqKc"/>
        <el-table-column show-overflow-tooltip label="物资规格" align="center" prop="wzGg"/>
        <el-table-column show-overflow-tooltip label="物资专业" align="center" prop="wzZy"/>
        <el-table-column show-overflow-tooltip label="单价" align="center" prop="dj"/>
        <el-table-column show-overflow-tooltip label="库存" align="center" prop="kc"/>
        <el-table-column show-overflow-tooltip label="物资备注" align="center" prop="wzBz"/>
        <!--      <el-table-column label="仓库关联状态" align="center" >-->
        <!--        <template slot-scope="scope">-->
        <!--          <el-link disabled v-if="scope.row.ssCk == null" style="color: red">未关联仓库</el-link>-->
        <!--          <el-link disabled v-else-if="scope.row.ssCk != null" style="color: #13ce66">正常关联</el-link>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
        <el-table-column label="仓库关联状态" align="center" prop="ckGlZt">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.wzgl_ckglzt" :value="scope.row.ckGlZt"/>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="所属部门" align="center" prop="deptName"/>
      </el-table>

      <!-- 分页组件 -->
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

    <div slot="footer" class="dialog-footer dialog-footer-center">
      <el-button size="small" @click="submitForm">
        <svg-icon icon-class="confirm1" class-name='custom-class' />确 定
      </el-button>
      <el-button size="small" @click="cancel">
        <svg-icon icon-class="cancel1" class-name='custom-class' />取 消
      </el-button>
    </div>

    <!-- 物资详情 -->
    <WzXq ref="wzXq"/>
  </vxe-modal>
</template>

<script>
import {listWzxx} from "@/api/wzgl/wzxx";
import WzXq from "@/views/wzgl/wzxx/components/WzXq.vue";

export default {
  name: "WzDX", //物资单选组件
  components: {WzXq},
  dicts: ['wzgl_ckglzt'],
  inject: ['updateWz'],
  data() {
    return {
      // 物资基本信息表格数据
      wzxxList: [],
      wzDuoX: false, //弹窗是否打开
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wzName: null,
        ssCk: null,
        ssHj: null,
        ssFc: null,
        wzFl: null,
        wzGg: null,
        wzZy: null,
        userId: null,
        deptId: null,
        ckName: null,
        hjName: null,
        fcName: null,
        ckGlZt: '正常关联',
      },
      // 显示搜索条件
      showSearch: true,
      wzLable: null,
      // 总条数
      total: 0,
      // 文件大小时间范围
      daterangeYxqZ: [],
      xzWzList: [], //选择的物资列表
      allSelected: [], // 存储所有选中的行
      // 选中数组
      ids: [],
    }
  },
  methods: {
    handleOpen(){
      this.queryParams.pageNum = 1
      this.wzDuoX = true;
      this.getList();
    },
    /** 查询物资基本信息列表 */
    getList() {
      this.loading = true;
      listWzxx(this.queryParams).then(response => {
        this.wzxxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    submitForm(){
      this.updateWz(this.xzWzList)
      this.wzDuoX = false;
    },
    cancel() {
      this.wzDuoX = false;
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
      this.wzLable = this.selectedRow
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.sbxxglId)
      if (ids.length === 1) {
        this.ids = ids[0]
      } else {
        this.ids = ids
      }
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    // 查看物资详情信息
    handleButtonClick(row) {
      this.$refs.wzXq.handleOpen(row);
    },

  }
}
</script>

<style scoped>

</style>
